//
//  AnimationUtil.cpp
//  TestReadPlist
//
//  Created by Joueu on 14-12-23.
//
//

#include "AnimationUtil.h"

AnimationUtil* AnimationUtil::_animationUtil = nullptr;

AnimationUtil* AnimationUtil::getInstance()
{
    if (! _animationUtil)
    {
        _animationUtil = new AnimationUtil();
    }
    return _animationUtil;
}

Animation* AnimationUtil::createAnimationWithFirstName( const char* firstName, float delay /*= 0.15f */, int loops/* = 1*/ )
{
//    auto tmpAnimation = AnimationCache::getInstance() -> getAnimation(firstName);
//    if(tmpAnimation != NULL)
//        return tmpAnimation;
    SpriteFrameCache *cache = SpriteFrameCache::getInstance();
    
    Vector<SpriteFrame *> frameVec;
    SpriteFrame *frame = NULL;
    int gap = 0;
    int index = 0;
    do {
        __String string = StringUtils::format("%s%d.png",firstName,index ++);
        frame = cache -> getSpriteFrameByName(string.getCString());
        if (frame == NULL) {
            gap++;
        }
        else {
            frameVec.pushBack(frame);
            gap = 0;
        }
    }while (gap < 10);
    
    Animation *animation = Animation::createWithSpriteFrames(frameVec);
    animation -> setLoops(loops);
    animation -> setDelayPerUnit(delay);
    animation -> setRestoreOriginalFrame(true);
    
//    AnimationCache::getInstance() -> addAnimation(animation, firstName);
    return animation;
}
